<?php 
/**
 * 大道CMS RBAC（这里全部写到这一个文件中，方便以后的项目挪用）
 * @author   tongjh
 * @version  V1.0
 */
class UserAction extends CommonAction {
	
	/* -----------------------------------用户管理------------------------------------------------ */
   /**
     * 用户列表
     * @access public
     * @return void
     */
    public function index(){
        $Form = M('user');
        import("@.ORG.Util.Page");
        $count = $Form->count();
	$p = new Page($count, 10);
        $list = $Form->limit($p->firstRow . ',' . $p->listRows)->order('id desc')->select();	
        $page = $p->show();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->display();	
    }

    /**
     * 用户增加
     * @access public
     * @return void
     */
    public function user_add(){
        $Form = D('User');
        if(IS_POST){
            $vo = $Form->create();                        
            if(!$vo){$this->error($Form->getError());}
            $Form->rand = get_rand(6);
            $Form->pwd = md5($_POST['pwd'].$Form->rand);
            if(false !== $id = $Form->add()){
                $this->success("增加成功");
            } else {
                $this->error("增加失败请检查参数");
            }exit();
        }
        $this->assign('groupid',  get_array('sysgroup'));
        $this->display();
   }
   
       /**
     * 用户修改
     * @access public
     * @return void
     */
    public function user_edit(){
            $map['id']=$_GET['id'];
            $Form = M('User');
            $list = $Form->where($map)->find();
            $this->assign('list',$list);
            $this->assign('groupid',  get_array('sysgroup'));
            $this->display();    	
   }

    /**
     * 用户更新
     * @access public
     * @return void
     */
    public function user_update(){
            $Form = D('User');
            $result = $Form->create();            
            if(!$result){$this->error($Form->getError());}
            //密码留空时不更新密码
            if(empty($_POST['pwd'])){
               unset($Form->pwd);
            }else{
                $rand = get_rand(6);
                $Form->rand = $rand;
                $Form->pwd = md5($_POST['pwd'].$rand);	
            }
            		  
            if(false !== $vo = $Form->where($map)->save()){
                $this->success("修改成功");
            } else {
                $this->error("修改失败");
            }	
    }
	  

	 
	 
   /**
     * 当前用户更改密码
     * @params $_GET['password']
     * @return void
     */
	  public function newpass(){
	  	$password = $this->_get('password');
	  	$rand = get_rand(6);
		$Form = M('User');
                $map['pwd'] = md5($password.$rand);
                $map['id'] = $_SESSION['userArr']['id'];
                $map['rand'] = $rand;
		if(false !== $Form->save($map)){
                    echo $password;
		}else{
                    echo "修改失败";
                }
	 }

	 /*
	* 用户删除
	* @return string
	*/
   public function user_del($id){
        $m = M('user');
        if(false === $m->delete($id)){
            $this->error('删除失败');
        }
        $this->success('删除成功');
   }
/* ---------------------------------节点管理--------------------------------------------------------- */
	/*
	* 节点管理
	* @return void
	*/
	public function jiedian(){
            if($_GET['delcache']){S('jiedian',NULL);echo "清除jiedian缓存成功";}
            if(!S('jiedian')){
                $map['topid'] = 0;
                $other['order'] = 'sort desc,id asc';
                $digui = digui('sysjiedian',$map,$other);
                S('jiedian',$digui);//缓存数据
                echo "首次访问建立缓存";
            }
            $this->assign('jiedian',S('jiedian'));                
            $this->display();
	}
        
        /*
         * 节点增加
         */
        public function jiedian_add(){            
            if(IS_POST){
                $form = D('Sysjiedian');
                $result = $form->create();
                if(!$result){$this->error($form->getError());}
                if(false === $id = $form->add()){
                    $this->error($form->getError());
                }
                $this->success("增加节点成功");exit();
            }
            $form = M('sysjiedian');
            $list = $form->order("topid asc,sort asc")->select();
            $topid = Tool::tree($list);
            $this->assign('topid',$topid);
            $this->display();
        }
        
    /**
     * 节点修改
     * @access public
     * @return void
     */
    public function jiedian_edit(){
        $id=$this->_get('id');
        $Form = M('sysjiedian');
        $list = $Form->where('id='.$id)->find();
        $this->assign('list',$list);
        $form2 = M('sysjiedian');
        $list2 = $form2->order("topid asc,sort asc")->select();
        $topid = Tool::tree($list2);
        $this->assign('topid',$topid);
        $this->display();    	
    }

    /*
    * 节点更新
    * @return string
    */
    public function jiedian_update(){
        $Form = D('Sysjiedian');
        if(!$Form->create()){
            $this->error($Form->getError());
        }
        if(false === $vo = $Form->save()){
            $this->error("修改失败");
        }
        $this->success("修改成功");
    }

	/*
	* 节点删除
	* @return string
	*/
   public function jiedian_del(){
   		//为了系统安全，请慎重删除节点
   		$map['id'] = $this->_post('id');
   		$Form = M('caozuo');
   		$num = $Form->where($map)->delete();
   		if(false !== $num){
   			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'删除失败，请检查参数'));
		}
    }

/* ---------------------------------用户组管理（角色）---------------------------------------------------- */
    
    /*
     * 用户组管理(角色)
     */
	public function sysgroup(){
		$this->assign('list',get_array('sysgroup'));
		$this->display();
	}

	/*
	* 用户组增加
	* @return string
	*/
	public function sysgroup_add(){
            $Form = D('Sysgroup');
            if(IS_POST){
                if(!$vo = $Form->create()){$this->error($Form->getError());}
                $Form->sys = implode(",", $this->_post('sys'));
                if(!$Form->sys){$this->error("请至少选择一个权限");}
                if(false !== $id = $Form->add()){
                        echo $this->success("增加成功");
                } else {
                        echo $this->error("增加失败");
                }exit();                    
            }
            //读取缓存
            $this->sysgroup_cache();
            $this->assign('jiedian',S('jiedian')); 
            $this->display();		
	}
        
        /*
	* 用户组修改
	* @return string
	*/
	public function sysgroup_edit(){
            $Form = M('sysgroup');
            $map['id'] = $this->_get('id');
            $list = $Form->where($map)->find();
            $this->assign('list',$list);
            //读取缓存
            $this->sysgroup_cache();
            $this->assign('jiedian',S('jiedian')); 
            $this->display();
	}

	/*
	* 用户组更新
	* @return string
	*/
	public function sysgroup_update(){
            $Form = D('Sysgroup');
            if(!$Form->create()){
                $this->error($Form->getError());                
            }
            $Form->sys = implode(",", $this->_post('sys'));
            if(!$Form->sys){$this->error("请至少选择一个权限");}
            if(false !== $vo = $Form->save()){
                $this->success("修改成功");
            } else {
                $this->error("修改失败，请检查参数");
            }
	}

	/*
	* 用户组删除
	* @return string
	*/
        public function sysgroup_del(){
            if(!($_SESSION['userArr']['name'] == "admin")){$this->error("请不要随意删除用户组，请联系技术部删除");}
            exit("需要删除功能时删除这一句");
            $map['id'] = $this->_post('id');
            $Form = M('sysgroup');
            $num = $Form->where($map)->delete();
            if(false !== $num){
                 $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
      
      /*
       * 用户组cache
       * 
       */
        protected function sysgroup_cache(){
              if($_GET['delcache']){S('jiedian',NULL);echo "清除jiedian缓存成功";}
              if(!S('jiedian')){
                  $map['topid'] = 0;
                  $other['order'] = 'sort desc,id asc';
                  $digui = digui('sysjiedian',$map,$other);
                  S('jiedian',$digui);//缓存数据
                  echo "首次访问建立缓存";
              }
        }


}
?>